package com.huawei.hms.support.api.pay.json;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import com.huawei.hms.api.Api;
import com.huawei.hms.api.HuaweiApiAvailability;
import com.huawei.hms.common.HuaweiApi;
import com.huawei.hms.core.aidl.IMessageEntity;
import com.huawei.hms.support.api.entity.pay.HwPayConstant;
import com.huawei.hms.support.api.entity.pay.HwWalletInfoRequest;
import com.huawei.hms.support.api.entity.pay.InternalPayRequest;
import com.huawei.hms.support.api.entity.pay.OrderRequest;
import com.huawei.hms.support.api.entity.pay.PayNaming;
import com.huawei.hms.support.api.entity.pay.PayReq;
import com.huawei.hms.support.api.entity.pay.ProductDetailRequest;
import com.huawei.hms.support.api.entity.pay.ProductPayRequest;
import com.huawei.hms.support.api.entity.pay.WalletUiIntentReq;
import com.huawei.hms.support.api.entity.pay.WithholdRequest;
import com.huawei.hms.support.api.pay.GetWalletUiIntentResult;
import com.huawei.hms.support.api.pay.HwWalletInfoResult;
import com.huawei.hms.support.api.pay.OrderResult;
import com.huawei.hms.support.api.pay.PayResult;
import com.huawei.hms.support.api.pay.PayResultInfo;
import com.huawei.hms.support.api.pay.ProductDetailResult;
import com.huawei.hms.support.api.pay.ProductPayResultInfo;
import com.huawei.hms.support.hianalytics.HiAnalyticsClient;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant.KeyAndValue;
import com.huawei.hms.support.log.HMSLog;
import com.huawei.hms.utils.Checker;
import com.huawei.hms.utils.JsonUtil;
import o.fjo;

public class PayClientImpl extends HuaweiApi<PayOptions> implements PayClient {
    private static final Api<PayOptions> a = new Api(HuaweiApiAvailability.HMS_API_NAME_PAY);
    private static PayOptions b = new PayOptions();

    PayClientImpl(Activity activity) {
        super(activity, a, b, new PayHmsClientBuilder());
    }

    PayClientImpl(Context context) {
        super(context, a, b, new PayHmsClientBuilder());
    }

    public fjo<PayResult> pay(PayReq payReq) {
        Checker.checkNonNull(payReq);
        return doWrite(new PayTaskApiCall(PayNaming.pay, JsonUtil.createJsonString(payReq), HiAnalyticsClient.reportEntry(getContext(), KeyAndValue.START_PAY)));
    }

    public PayResultInfo getPayResultInfoFromIntent(Intent intent) {
        if (intent == null) {
            HMSLog.e("PayClientImpl", "getPayResultInfoFromIntent intent is null");
            return null;
        }
        Bundle extras = intent.getExtras();
        if (extras == null) {
            HMSLog.e("PayClientImpl", "getPayResultInfoFromIntent bundle is null");
            return null;
        }
        PayResultInfo payResultInfo = new PayResultInfo();
        payResultInfo.setReturnCode(extras.getInt("returnCode"));
        payResultInfo.setUserName(extras.getString(HwPayConstant.KEY_USER_NAME));
        payResultInfo.setOrderID(extras.getString("orderID"));
        payResultInfo.setAmount(extras.getString(HwPayConstant.KEY_AMOUNT));
        payResultInfo.setErrMsg(extras.getString("errMsg"));
        payResultInfo.setTime(extras.getString("time"));
        payResultInfo.setCountry(extras.getString(HwPayConstant.KEY_COUNTRY));
        payResultInfo.setCurrency(extras.getString(HwPayConstant.KEY_CURRENCY));
        payResultInfo.setWithholdID(extras.getString("withholdID"));
        payResultInfo.setRequestId(extras.getString(HwPayConstant.KEY_REQUESTID));
        payResultInfo.setSign(extras.getString(HwPayConstant.KEY_SIGN));
        HMSLog.i("PayClientImpl", "final pay result info::" + payResultInfo.getReturnCode());
        return payResultInfo;
    }

    public fjo<ProductDetailResult> getProductDetails(ProductDetailRequest productDetailRequest) {
        Checker.checkNonNull(productDetailRequest);
        return doWrite(new ProductDetailTaskApiCall(PayNaming.productdetail, JsonUtil.createJsonString(productDetailRequest)));
    }

    public fjo<PayResult> productPay(ProductPayRequest productPayRequest) {
        Checker.checkNonNull(productPayRequest);
        return doWrite(new PayTaskApiCall(PayNaming.pms, JsonUtil.createJsonString(productPayRequest), HiAnalyticsClient.reportEntry(getContext(), PayNaming.pms)));
    }

    public ProductPayResultInfo getProductPayResultFromIntent(Intent intent) {
        if (intent == null) {
            HMSLog.e("PayClientImpl", "getProductPayResultFromIntent intent is null");
            return null;
        }
        Bundle extras = intent.getExtras();
        if (extras == null) {
            HMSLog.e("PayClientImpl", "getProductPayResultFromIntent bundle is null");
            return null;
        }
        ProductPayResultInfo productPayResultInfo = new ProductPayResultInfo();
        productPayResultInfo.setReturnCode(extras.getInt("returnCode"));
        productPayResultInfo.setOrderID(extras.getString("orderID"));
        productPayResultInfo.setErrMsg(extras.getString("errMsg"));
        productPayResultInfo.setProductNo(extras.getString(HwPayConstant.KEY_PRODUCT_NO));
        productPayResultInfo.setMicrosAmount(extras.getLong("microsAmount"));
        productPayResultInfo.setTime(extras.getString("time"));
        productPayResultInfo.setCountry(extras.getString(HwPayConstant.KEY_COUNTRY));
        productPayResultInfo.setCurrency(extras.getString(HwPayConstant.KEY_CURRENCY));
        productPayResultInfo.setRequestId(extras.getString(HwPayConstant.KEY_REQUESTID));
        productPayResultInfo.setMerchantId(extras.getString(HwPayConstant.KEY_MERCHANTID));
        productPayResultInfo.setSign(extras.getString(HwPayConstant.KEY_SIGN));
        HMSLog.i("PayClientImpl", "final product pay result info::" + productPayResultInfo.getReturnCode());
        return productPayResultInfo;
    }

    public fjo<PayResult> addWithholdingPlan(WithholdRequest withholdRequest) {
        Checker.checkNonNull(withholdRequest);
        return doWrite(new PayTaskApiCall(PayNaming.withhold, JsonUtil.createJsonString(withholdRequest), HiAnalyticsClient.reportEntry(getContext(), PayNaming.pms)));
    }

    public fjo<OrderResult> getOrderDetail(OrderRequest orderRequest) {
        Checker.checkNonNull(orderRequest);
        return doWrite(new OrderTaskApiCall(PayNaming.getOrderDetail, JsonUtil.createJsonString(orderRequest)));
    }

    public fjo<PayResult> internalPay(InternalPayRequest internalPayRequest) {
        Checker.checkNonNull(internalPayRequest);
        return doWrite(new PayTaskApiCall(PayNaming.internalPay, JsonUtil.createJsonString(internalPayRequest), HiAnalyticsClient.reportEntry(getContext(), PayNaming.internalPay)));
    }

    public fjo<HwWalletInfoResult> queryWalletInfo(HwWalletInfoRequest hwWalletInfoRequest) {
        Checker.checkNonNull(hwWalletInfoRequest);
        return doWrite(new HwWalletInfoTaskApiCall(PayNaming.walletQuery, JsonUtil.createJsonString(hwWalletInfoRequest)));
    }

    public fjo<GetWalletUiIntentResult> getWalletUiIntent(int i) {
        IMessageEntity walletUiIntentReq = new WalletUiIntentReq();
        walletUiIntentReq.setType(i);
        Checker.checkNonNull(walletUiIntentReq);
        return doWrite(new GetWalletUiIntentTaskApiCall(PayNaming.getwalletintent, JsonUtil.createJsonString(walletUiIntentReq)));
    }
}
